﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserLogin_ProjectManager_AddProject : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MyDropDownListDataBind();
        }

        projectManager.Text = ((Label)Master.FindControl("LabelUserInfo")).Text;
        
    }

    protected  void MyDropDownListDataBind()
    {
        //Language1
        var results1 = from r in db.UserInfo
                       where r.Skill.Contains("C语言")==true
                       select r.Name;
        language1.DataSource = results1;
        language1.DataBind();
        language1.Items.Insert(0, new ListItem("C语言工程师", "Null"));
        

        //Languagw2
        var results2 = from r in db.UserInfo
                       where r.Skill.Contains("C++") == true
                       select r.Name ;
        language2.DataSource = results2;
        language2.DataBind();
        language2.Items.Insert(0, new ListItem("C++工程师", "Null"));

        //Language3
        var results3 = from r in db.UserInfo
                       where r.Skill.Contains("C#") == true
                       select r.Name;
        language3.DataSource = results3;
        language3.DataBind();
        language3.Items.Insert(0, new ListItem("C#工程师", "Null"));

        //Language4
        var results4 = from r in db.UserInfo
                       where r.Skill.Contains("Java") == true
                       select r.Name ;
        language4.DataSource = results4;
        language4.DataBind();
        language4.Items.Insert(0, new ListItem("Java工程师", "Null"));

        //Language5
        var results5 = from r in db.UserInfo
                       where r.Skill.Contains("Python") == true
                       select r.Name ;
        language5.DataSource = results5;
        language5.DataBind();
        language5.Items.Insert(0, new ListItem("Python工程师", "Null"));

        //Language6
        var results6 = from r in db.UserInfo
                       where r.Skill.Contains("PHP") == true
                       select r.Name ;
        language6.DataSource = results6;
        language6.DataBind();
        language6.Items.Insert(0, new ListItem("PHP工程师", "Null"));

    }

    protected void btnChangeSkill_Click(object sender, EventArgs e)
    {
        btnChangeSkill.Visible = false;
        btnFinishSkill.Visible = true;
        btnChangeMember.Visible = false;

        SkillPanel.Visible = true;
        MemberPanel.Visible = false;

        projectMember.Text = "";
    }
    protected void btnFinishSkill_Click(object sender, EventArgs e)
    {
        btnChangeSkill.Visible = true;
        btnFinishSkill.Visible = false;
        btnChangeMember.Visible = true;

        SkillPanel.Visible = false;
        MemberPanel.Visible = true ;

        foreach (ListItem listItem in CheckBoxList1.Items)
        {
            if (listItem.Selected)
            {
                string text = listItem.Text;

                if (text == "C语言")
                    language1.Visible = true;
                else if (text == "C++")
                    language2.Visible = true;
                else if (text == "C#")
                    language3.Visible = true;
                else if (text == "Java")
                    language4.Visible = true;
                else if (text == "Python")
                    language5.Visible = true;
                else if (text == "PHP")
                    language6.Visible = true;

            }
            else
            {
                string text = listItem.Text;

                if (text == "C语言")
                    language1.Visible = false;
                else if (text == "C++")
                    language2.Visible = false;
                else if (text == "C#")
                    language3.Visible = false;
                else if (text == "Java")
                    language4.Visible = false;
                else if (text == "Python")
                    language5.Visible = false;
                else if (text == "PHP")
                    language6.Visible = false;
            }
        }

    }


    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedText = "";

        foreach(ListItem listitem in CheckBoxList1.Items)
        {
            if (listitem.Selected)
            {
                selectedText += listitem.Text + "  ";   
            }
                
        }

        projectSkill.Text = selectedText;
    }





    protected void language1_SelectedIndexChanged(object sender, EventArgs e)
    {
        languageDropDownListRefresh(sender);
    }
    protected void language2_SelectedIndexChanged(object sender, EventArgs e)
    {
        languageDropDownListRefresh(sender);
    }
    protected void language3_SelectedIndexChanged(object sender, EventArgs e)
    {
        languageDropDownListRefresh(sender);
    }
    protected void language4_SelectedIndexChanged(object sender, EventArgs e)
    {
        languageDropDownListRefresh(sender);
    }
    protected void language5_SelectedIndexChanged(object sender, EventArgs e)
    {
        languageDropDownListRefresh(sender);
    }
    protected void language6_SelectedIndexChanged(object sender, EventArgs e)
    {
        languageDropDownListRefresh(sender);
    }
    protected void languageDropDownListRefresh(object sender)
    {
        foreach (ListItem listItem in ((DropDownList)sender).Items)
        {
            if (listItem.Selected&&listItem.Value!="Null")
            {
                if(projectMember.Text.Contains(listItem.Text)==false)
                    projectMember.Text += listItem.Text + " ";

                listItem.Selected = false;
            }
        }

    }




    protected void btnCreate_Click(object sender, EventArgs e)
    {

        if(Page.IsValid)
        {
            ProjectInfo projectInfo = new ProjectInfo();
            projectInfo.ProjectName = projectName.Text;
            projectInfo.Type = projectLevel.SelectedItem.Text;
            projectInfo.ProjectManager = projectManager.Text;
            projectInfo.StartTime = projectStartTime.Text;
            projectInfo.EndTime = projectEndTime.Text;
            projectInfo.Skill = projectSkill.Text;
            projectInfo.ProjectMember = projectMember.Text;
            projectInfo.Background = projectBackground.Text;
            projectInfo.Status = null;

            db.ProjectInfo.InsertOnSubmit(projectInfo);
            db.SubmitChanges();

            Response.Redirect("ProjectManagerIndex.aspx");
        }
       
    }

    protected void btnChangeMember_Click(object sender, EventArgs e)
    {
        MemberPanel.Visible = false;

        btnChangeMember.Visible = false;
        btnClear.Visible = true;
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        MemberPanel.Visible = true;

        projectMember.Text = "";

        btnChangeMember.Visible = true;
        btnClear.Visible = false;
    }
}